package org.example.wu_and_dian.controller;

import org.example.wu_and_dian.annotation.TokenRequired;
import org.example.wu_and_dian.dto.response.AjaxResult;
import org.example.wu_and_dian.entity.Entity;
import org.example.wu_and_dian.service.DocumentService;
import org.example.wu_and_dian.service.EntityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class EntityController {
    @Autowired
    private EntityService entityService;
    @TokenRequired
    @PostMapping("/create_entity_instance")
    public AjaxResult createEntityInstance(@RequestBody Entity entity){
        return entityService.createEntityInstance(entity);
    }
    @TokenRequired
    @PostMapping("/delete_entity_instance")
    public AjaxResult deleteEntityInstance(@RequestBody Map<String,String> request) {
        String documentId = request.get("documentId");
        String instanceId = request.get("instanceId");
        boolean success = entityService.deleteById(documentId,instanceId);
        return success ? AjaxResult.success("删除实体实例成功") : AjaxResult.error("实体实例不存在");
    }
    @TokenRequired
    @PutMapping("/update_entity_instances_by_entity_id")
    public AjaxResult updateEntityInstancesByEntityId(@RequestBody Map<String, Object> request) {
        String documentId = (String) request.get("documentId");
        String entityId = (String) request.get("entityId");
        Boolean isChecked =(Boolean) request.get("isChecked");
        return entityService.updateEntityInstancesByEntityId(documentId, entityId, isChecked);
    }
    @TokenRequired
    @PutMapping("/update_entity_instance")
    public AjaxResult updateEntityInstances(@RequestBody Map<String, Object> request) {
        String instanceId = (String)request.get("instanceId");
        String documentId = (String)request.get("documentId");
        Boolean isChecked = (Boolean)request.get("isChecked");
        return entityService.updateEntityInstance(instanceId, documentId, isChecked);
    }
    @TokenRequired
    @GetMapping("/get_entity_instances/{documentId}")
    public AjaxResult getEntityInstanceByDocumentId(@PathVariable String documentId) {
        List<Entity> entities = entityService.getByDocumentId(documentId);
        List<Map<String, String>> responseData = entities.stream()
                .map(entity -> Map.of("id", entity.getId().toString(), "text", entity.getText(), "entity_id",entity.getEntityId()))
                .toList();
        return AjaxResult.success("获取实体实例成功", Map.of("entity_instances", responseData));
    }
    @TokenRequired
    @PostMapping("/delete_entity_instances_by_entity_id")
    public AjaxResult deleteEntityInstancesByEntityId(@RequestBody Map<String, String> request) {
        String documentId = request.get("documentId");
        String instanceId = request.get("instanceId");
        return entityService.deleteEntityInstancesByEntityId(documentId, instanceId);
    }
}
